package com.prp.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.prp.dao.Userinfo;
import com.prp.service.ServiceFactory;

public class UploadFilesAction extends ActionSupport {

	
	private File[] Filedata;
	private String[] FiledataFileName;
	
	private File content_file;	
	private String content_fileFileName;
	private String content_fileContentType;
	
	
	private File[] files;	
	private String[] filesFileName;
	private String[] filesContentType;
	
	private String code;
	private String up_success;
	private String up_fail_message;
	
	/**
	 * use swfupload to uploads files
	 * @return
	 */
	public String execute()  throws IOException{
		// TODO Auto-generated method stub
		
		
		ActionContext ac = ActionContext.getContext();		
		Map para = ac.getParameters();
		
		String[] username = (String[])para.get("user_id");
		String[] file_id = (String[])para.get("file_id");

		String[] is_end = (String[])para.get("is_end");
		boolean flag = false;
		if (is_end != null && is_end[0].equals("true"))
			flag = true;
		
		
		
		
		HttpServletResponse response = ServletActionContext.getResponse();		
		response.setCharacterEncoding("UTF-8");    		       
		PrintWriter out = response.getWriter();
		HashMap map = new HashMap();

		
		
		if (ServiceFactory.getFileManage().uploadFiletype2(username[0]
		                   , Filedata[0], FiledataFileName[0], file_id[0]) == -1)
			map.put("success", "false");
		else
			map.put("success", "true");
		

		int r2 = 0;
		map.put("is_end", "false");
		if (flag)
		{
			r2 = ServiceFactory.getFileManage().completeUploadType2(username[0], ac.getSession(), map);
			map.put("is_end", "true");
		}
		
				
		
		JSONObject json = JSONObject.fromObject(map);
		
		
		out.print(json.toString());
		out.flush();
		out.close();
		
			
		return null;
	}
	/*
	 * use only html
	 */
	public String type1()
	{
		String username = (String) ActionContext.getContext().getSession().get("username");
		if (username == null)
		{
			return INPUT;
		}
		
		try {
			
			String[] files_id = new String[files.length];
			HashMap map = new HashMap();
			files_id[0] = "id_file_1";
			ServiceFactory.getFileManage().uploadFiles(username, content_file, files, filesFileName, files_id, map);
			String str = (String) map.get("id_file_1");
			if (str != null){
				if (str.equals("success")){
					up_success= "true";
				}else{
					up_success = "false";
					this.up_fail_message = this.getText(str);
				}
			}else{
				this.up_success = "false";
				this.up_fail_message = this.getText("error_5");
			}
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	public String endUpload() throws IOException
	{
		ActionContext ac = ActionContext.getContext();		
		Map para = ac.getParameters();
		Object o = para.get("code");
		Map sesson = ac.getSession();
		
		ServletActionContext.getResponse().setContentType("text/html"); 
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		
		HashMap map = new HashMap();
		map.put("success", "true");
		JSONObject j = JSONObject.fromObject(map);
		ServletActionContext.getResponse().getWriter().printf(j.toString());
		ServletActionContext.getResponse().getWriter().flush();
		ServletActionContext.getResponse().getWriter().close();
		
		return null;
	}
	public void setFiledataFileName(String[] filedataFileName) {
		FiledataFileName = filedataFileName;
	}
	public String[] getFiledataFileName() {
		return FiledataFileName;
	}
	
	public void setContent_file(File content_file) {
		this.content_file = content_file;
	}
	public File getContent_file() {
		return content_file;
	}
	public void setContent_fileFileName(String content_fileFileName) {
		this.content_fileFileName = content_fileFileName;
	}
	public String getContent_fileFileName() {
		return content_fileFileName;
	}
	public void setContent_fileContentType(String content_fileContentType) {
		this.content_fileContentType = content_fileContentType;
	}
	public String getContent_fileContentType() {
		return content_fileContentType;
	}
	public void setFiles(File[] files) {
		this.files = files;
	}
	public File[] getFiles() {
		return files;
	}
	public void setFilesFileName(String[] filesFileName) {
		this.filesFileName = filesFileName;
	}
	public String[] getFilesFileName() {
		return filesFileName;
	}
	public void setFilesContentType(String[] filesContentType) {
		this.filesContentType = filesContentType;
	}
	public String[] getFilesContentType() {
		return filesContentType;
	}
	public void setFiledata(File[] filedata) {
		Filedata = filedata;
	}
	public File[] getFiledata() {
		return Filedata;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getCode() {
		return code;
	}
	public void setUp_success(String up_success) {
		this.up_success = up_success;
	}
	public String getUp_success() {
		return up_success;
	}
	public void setUp_fail_message(String up_fail_message) {
		this.up_fail_message = up_fail_message;
	}
	public String getUp_fail_message() {
		return up_fail_message;
	}
}